using MallSystembackend.Api.Repository;
using MallSystembackend.Api.Model;
using Microsoft.AspNetCore.Mvc;
using MallSystembackend.Api.ParamModel;
using Microsoft.Extensions.Configuration;
namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class MerchantController : ControllerBase
    {
        public IRepository<Merchant> _merchantRepository;
        public readonly IConfiguration _configuration;

        public MerchantController(IRepository<Merchant> merchantRepository
        , IConfiguration configuration)
        {
            _merchantRepository = merchantRepository;
            _configuration = configuration;
        }
        //添加店铺
        [HttpPost]
        public ActionResult AddMerchant(ParamMerchant model)
        {
            var NewMerchant = new Merchant
            {
                MerchantName = model.MerchantName,
                levelId = model.levelId,
                UsersId = model.UsersId
            };
            _merchantRepository.Insert(NewMerchant);
            return Ok(new
            {
                Code = 1000,
                Data = NewMerchant,
                Msg = "添加成功"
            });

        }
        //删除店铺
        [HttpDelete("{id}")]
        public ActionResult DeletedMerchant(int id)
        {
            var getMerchant = _merchantRepository.GetById(id);
            if (getMerchant != null)
            {
                getMerchant.IsDeleted = true;
                _merchantRepository.Update(getMerchant);
                return Ok(new
                {
                    Code = 1000,
                    Data = getMerchant,
                    Msg = "删除成功"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = "",
                Msg = "没有找到该店铺"
            });
        }
        //获得店铺
        [HttpGet]
        public ActionResult GetAny()
        {
            var AnyMerchant = _merchantRepository.Table;
            return Ok(new
            {
                Code = 1000,
                Data = AnyMerchant,
                Msg = "获得成功"
            });
        }
        //修改店铺
        [HttpPut("{id}")]
        public ActionResult UpdateMerchant(int id, ParamMerchant model)
        {
            var UpdateMerchantone = _merchantRepository.GetById(id);
            if (UpdateMerchantone != null)
            {
                UpdateMerchantone.MerchantName = model.MerchantName;
                UpdateMerchantone.levelId = model.levelId;
                UpdateMerchantone.UsersId = model.UsersId;
                _merchantRepository.Update(UpdateMerchantone);
                return Ok(new
                {
                    Code = 1000,
                    Data = UpdateMerchantone,
                    Msg = "修改成功"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = "",
                Msg = "修改失败，未找到店铺"
            });
        }
        //根据id获得店铺
        [HttpGet("{id}")]
        public ActionResult GetMerchant(int id)
        {
            var Merchant = _merchantRepository.GetById(id);
            if (Merchant != null)
            {
                return Ok(new
                {
                    Code = 1000,
                    Data = Merchant,
                    Msg = "获得成功"
                });
            }
            return Ok(new {
                Code = 1002,
                Data="",
                Msg ="获得失败"
            });
        }
        //操作店铺状态
        [HttpPut("IsActived/{id}")]
        public ActionResult UpdateActived(int id)
        {
            var UpdateMerchantone = _merchantRepository.GetById(id);
            if (UpdateMerchantone != null)
            {
                if (UpdateMerchantone.IsActived == true)
                {
                    UpdateMerchantone.IsActived = false;
                    _merchantRepository.Update(UpdateMerchantone);
                }
                else
                {
                    UpdateMerchantone.IsActived = true;
                    _merchantRepository.Update(UpdateMerchantone);
                }

                return Ok(new
                {
                    Code = 1000,
                    Data = UpdateMerchantone,
                    Msg = "修改成功"
                });
            }
            return Ok(new
            {
                Code = 1000,
                Data = "",
                Msg = "修改失败"
            });
        }
    }
}